package com.jielihaofeng.springboot.websocket.controller;

import com.jielihaofeng.springboot.websocket.config.WebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.Set;

/**
 * @author Johnnie Wind
 * @description
 * @date 2021/4/7 11:11
 */
@Controller
public class WebSocketController {

    @Autowired
    WebSocket webSocket;

    @ResponseBody
    @GetMapping("/sendTo")
    public String sendTo(@RequestParam("userId") String userId, @RequestParam("msg") String msg) throws Exception {
        webSocket.sendMessageTo(msg, userId);
        return "推送成功";
    }

    @ResponseBody
    @GetMapping("/sendAll")
    public String sendAll(@RequestParam("msg") String msg) throws IOException {

        String fromUserId = "system";
        webSocket.sendMessageAll(msg, fromUserId);

        return "推送成功";
    }

    /**
     * @description 获取在线用户ID
     * @author Johnnie Wind
     * @date 2021/4/7 12:23
     * @Param []
     * @return java.lang.String
     */
    @GetMapping("/onlineUser")
    public Set<String> getOnlineUserIds(){
        return WebSocket.getOnlineUserIds();
    }
}
